<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("./_inc/dataAreaFunction.php");

$errMsg = "";
$dataArea = "";
$description = "";
$note = "";
if(sxREQ("reqtype") == "save"){
	$errMsg .= dataAreaMod();
}elseif(sxREQ("reqtype") == "delete"){
	$errMsg .= dataAreaDel();
}
$dataArea = sxREQ("da");

$sSQL = "SELECT da.*, p.description AS p_descr, pda.permission " .
				"FROM data_area da " .
				"LEFT JOIN data_area_permission pda " .
				"ON pda.data_area = da.data_area " .
				"LEFT JOIN profile p " .
				"ON p.id_profile = pda.id_profile " .
				"WHERE da.data_area = '".$dataArea."' " .
				"ORDER BY pda.permission ";
$objSDAO->query($sSQL, "da");
$row = $objSDAO->fetch("da");

//seleziono i doc associati all'area dati
$sSQL = "SELECT * FROM data_area_doc WHERE data_area='$dataArea'";
$objSDAO->query($sSQL, "doc");

//output HTML
$aOpz = array("kMenu"=>'dam');
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
  
<?php
$aCmd1 = array(	
					"mod"=>array("pag"=>"dataAreaMod.php?da=$dataArea",
										"title"=>_t("data-area change"),
										"text"=>_t("change"),
										"perm"=>"M"),
					"del"=>array("pag"=>"javascript:confirmDelete();",
										"title"=>_t("data-area delete"),
										"text"=>_t("delete"),
										"perm"=>"M"),
					"user"=>array("pag"=>"dataAreaProfilesIns.php?da=$dataArea",
										"title"=>_t("enable profile"),
										"text"=>_t("enable profile"),'align'=>'L',
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1)
										)
					);
echo(commandsHTML($aCmd1));	
// data entry form
echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('da');
echo getHtmlHidden('data_area',sxREQ('da'));
echo getHtmlHidden('reqtype','...');
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>"._t("Data-Area")."</td>" .
			"<td class=\"titl01\" colspan='1'>"._t("Documents")."</td>" .
"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"10%\">"._t("Name")."</td>" .
			"<td class=\"text01\" width=\"50%\">$dataArea".
			"<td class=\"text01\" width=\"40%\" rowspan='5' valign='top'>");
$numDoc = 0;
while($rowDoc = $objSDAO->fetch("doc")){
	$numDoc++;
	if(file_exists(SERVER_FS_DOC."/mb_data_area/$dataArea/".$rowDoc["file_name"])){
		echo("$numDoc) <a class=\"text01\" target=\"blank\" "
			."href=\"getDoc.php?da=$dataArea&fn=".$rowDoc["file_name"]."&xsn=".XSN."\">"
			.sxHE($rowDoc["name"])."</a><br/>\n");
	} else {
		$sSql = "DELETE FROM data_area_doc WHERE data_area='$dataArea' AND file_name = '".sxHE($rowDoc["file_name"])."'";
	}
}
if($numDoc == 0){
	echo(_t("No documents"));
}
echo ("</td></tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")."</td>" .
			"<td class=\"text01\">".sxHE($row['description'])."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($row['note']))."</pre></td>" .
		"</tr>\n");
$aProf=array();
while($row and $row['permission'] == 'M') {
	$aProf[]= $row['p_descr'];
	$row = $objSDAO->fetch("da");
}
if ($aProf) {
	echo("<tr>\n<td class=\"labl01\">"._t("Enabled profiles")."</td>\n" );
	echo("<td class=\"text01\">".implode(', ',$aProf)."</td>\n</tr>\n");
}
$aProf=array();
while($row and $row['permission'] == 'N') {
	$aProf[]= $row['p_descr'];
	$row = $objSDAO->fetch("da");
}
if ($aProf) {
	echo("<tr>\n<td class=\"labl01\">"._t("Disabled profiles")."</td>\n" );
	echo("<td class=\"text01\">".implode(', ',$aProf)."</td>\n</tr>\n");
}
echo("</table>\n");
echo("</form>");
// subject-area output 
$aCmd2 = array(	
					"relpad"=>array("pag"=>"../rela/parentRelView.php?da=$dataArea",
										"title"=>_t("parent-child relationships"),
										"text"=>_t("parent-child relationships"),
										"perm"=>"M",'align'=>'L'),
					"relfig"=>array("pag"=>"../rela/childRelView.php?da=$dataArea",
										"title"=>_t("child-parent relationships"),
										"text"=>_t("child-parent relationships"),
										"perm"=>"M",'align'=>'L'),
					"inssa"=>array("pag"=>"subjectAreaIns.php?da=$dataArea",
										"title"=>_t("Insert a new subject-area"),
										"text"=>_t("new subject-area"),
										"perm"=>"M"),
					);
echo(commandsHTML($aCmd2));	
$numElem = 0;
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\"colspan=\"4\">".
			_t("Subject-Areas")."</td>" .
	 "</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\"><a class=\"tabh01\" ".
			goToPag("../enty/entityList.php?da=".$row["data_area"]."&xsn=".XSN).
			" title=\""._t("List of all entities of data-area")."\">".
			"<img src='".SERVER_URL_ROOT."/app/_img/b_browse.png' border='0'>".
			"</a></td>" .
		"<td class=\"tabh01\" width=\"10%\">"._t("Subject-Area")."</td>" .
		"<td class=\"tabh01\" width=\"49%\">"._t("Description")."</td>" .
		"<td class=\"tabh01\" width=\"40%\">"._t("Note")."</td>" .
	"</tr>\n");
$sSQL = "SELECT sa.*, da.description AS da_description FROM subject_area sa " .
		" INNER JOIN data_area da ON da.data_area = sa.data_area " .
		" WHERE sa.data_area = '$dataArea' " .
		" ORDER BY sa.subject_area ";
$query = $objSDAO->query($sSQL,'subAre');
while($row = $objSDAO->fetch('subAre')){
	$numElem ++;
	echo("<tr>" .
		"<td class=\"text01\" valign='top'><a class=\"text01\" ".
			goToPag("../enty/entityList.php?da=".$row["data_area"]."&sa=".$row["subject_area"]."&xsn=".XSN).
			" title=\""._t("List of entities of")." '".$row["description"]."'\">".
			"<img src='".SERVER_URL_ROOT."/app/_img/b_browse.png' border='0'>".
			"</a></td>\n" .
		"<td class=\"text01\" valign='top'><a class=\"text01\" ".
			goToPag("subjectAreaMod.php?sa=".$row["subject_area"]."&da=".$row["data_area"]."&xsn=".XSN).
			" title=\""._t("Modify subject area")."\">".$row["subject_area"]."</a></td>" .
		"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
		"<td class=\"text01\" valign='top'><pre class='text01'>".sxHE($row["note"])."</pre></td>" .
	"</tr>\n");
}
if($numElem == 0)
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"4\">"._t("No subject-area")."</td>".
		"</tr>\n");
echo("</table>\n");
echo(commandsHTML($aCmd2));

echo(footerHTML());
exit();

function dataAreaDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["data_area"]) || $_REQUEST["data_area"] == "")
		$errMsg .= _t("data-area missing");
	$dataArea = $_REQUEST["data_area"];
	//verifico che la data_area non sia usata
	$sSQL = "SELECT count(*) AS num FROM entity WHERE data_area='".$_REQUEST["data_area"]."'";
	$objSDAO->query($sSQL);
	$row = $objSDAO->fetch();
	if($row["num"] >0){
		$errMsg .= _t("L'area dati contiene entity. Non &egrave; possibile eliminarla.")."<br>";
	}
	//delete docs
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "SELECT * FROM data_area_doc WHERE data_area='$dataArea'";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()){
		if(file_exists(SERVER_FS_DOC."/mb_data_area/$dataArea/".$row["file_name"])){
			@unlink(SERVER_FS_DOC."/mb_data_area/$dataArea/".$row["file_name"]);
		}	
	}
	$sSQL = "DELETE FROM data_area_doc WHERE data_area='$dataArea'";
	$objSDAO->exec($sSQL);
	//delete folder
	if (is_dir(SERVER_FS_DOC."/mb_data_area/$dataArea")) {
		@rmdir(SERVER_FS_DOC."/mb_data_area/$dataArea");
	}
	$sSQL = "DELETE FROM data_area " .
			"WHERE data_area='".$_REQUEST["data_area"]."'";	
	$objSDAO->exec($sSQL);
	//delete profile association
	profDAUpd("del");
	$_SESSION[USEROBJ]->reLoad();
	logRecord("500", _t("Data-Area")." '".sxREQ("data_area")."' "._t("deleted"));
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	exit();
}
